from __future__ import division

from gameset.phoenicia.base import *


class Fruit(Food):
    singular =_ ('Fruit')
    plural   =_ ('Fruit')


class FruitBearer(Building, FertilityDependent):
    growth_days   =_ (None)
    decay_days    =_ (None)
    tend_days     =_ (None)
    tend_modifier =_ (None)
    good          =_ (None)
    
    is_passable =_ (True)
    
    tended   = Attribute(initial = 0, writable = True, clamp = (0, None))
    ripeness = Attribute(initial = 0, writable = True, clamp = (0, 1))
    decay    = Attribute(initial = 0, writable = True, clamp = (0, 1))
    
    @property
    def efficiency(self):
        if self.tended:
            return self.fertility_modifier
        else:
            return self.fertility_modifier / self.tend_modifier
    
    @property
    def riping_per_day(self):
        return self.efficiency / self.growth_days
    
    @property
    def decay_per_day(self):
        if self.tended:
            return 1 / self.decay_days / self.tend_modifier
        else:
            return 1 / self.decay_days
    
    @property
    def goods(self):
        return self.good * (self.ripeness - self.decay)
    
    @property
    def is_ripe(self):
        return self.ripeness == 1
    
    @property
    def is_decayed(self):
        return self.decay == 1
    
    def harvest_into(self, storage):
        storage.add_goods(self.goods)
        self.discard()
    
    def discard(self):
        self.ripeness = 0
        self.decay    = 0
    
    def day(self, day, month, year):
        if self.is_ripe:
            self.decay    += self.decay_per_day
        else:
            self.ripeness += self.riping_per_day
        
        self.tended -= 1
    
    def tend(self):
        self.tended = self.tend_days
    
    @property
    def state(self):
        return {'Fertility' : lambda: '%d%%' % (self.fertility * 100),
                'Tended for': lambda: '%dd'  % self.tended,
                'Ripeness'  : lambda: '%d%%' % (self.ripeness * 100),
                'Decay'     : lambda: '%d%%' % (self.decay    * 100),
                'Harvest'   : lambda: str(self.goods)}


class Planter(Seeker):
    color    =_ (Green.dark + Black)
    speed    =_ (8)
    radius   =_ (5)
    capacity =_ (50)
    
    def on_idle(self):
        if self.one_in(200):
            self.move_home()
        else:
            self.move_to_plant()
    
    def on_arrival_home(self):
        if self.storage.is_empty:
            self.destroy()
        else:
            self.unload_into(self.home)
    
    def move_to_plant(self):
        self.move_to_random(self.tile.nearby(FruitBearer, radius = self.radius))
    
    def harvest(self, plant):
        plant.harvest_into(self.storage)
    
    def day(self, day, month, year):
        for plant in self.tile.nearby(FruitBearer, radius = 2):
            if plant.is_decayed:
                plant.discard()
            elif plant.is_ripe and self.can_hold_all(plant.goods):
                self.harvest(plant)
                self.move_home()
            else:
                plant.tend()


class Plantation(StaffedBuilding):
    name       =_ ('Plantation')
    size       =_ (5)
    cost       =_ (1200)
    staff      =_ (64)
    agents     =_ (4)
    profession =_ (Planter)
    color      =_ (Green.light.lighter + Black)
    stores     =_ (Fruit(1000))
    supply     =_ ([Fruit])
    
    @property
    def spawn_point(self):
        return self.access_point
